package com.wt.struggle.goodbye.main.adapter

import android.content.Context
import android.graphics.BitmapFactory
import android.support.constraint.ConstraintLayout
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.ProBaseAdapter
import com.wt.struggle.goodbye.info.FileInfo
import com.wt.struggle.goodbye.main.activity.SendTieActivity
import com.xin.lv.yang.utils.utils.ImageUtil
import kotlinx.android.synthetic.main.image_view_layout.view.*

class PicAdapter(context: Context, list: ArrayList<FileInfo>) : ProBaseAdapter<FileInfo>(context, list) {

    /**
     *  3 为表情包   2 为从我的帖子中跳转  1 为私信获取申请   4 我的相册图片信息
     */
    var code = 1

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val vh = holder as VH
        val fileInfo = list[position]

        when (code) {
            1 -> {
                if (position == list.size - 1) {
                    vh.imageViewDelete.visibility = View.GONE
                    vh.imageView.setImageBitmap(BitmapFactory.decodeResource(context.resources, fileInfo.text.toInt()))
                } else {
                    vh.imageViewDelete.visibility = View.VISIBLE
                    ImageUtil.getInstance()
                        .loadRoundCircleImage(context, vh.imageView, fileInfo.text, R.drawable.em_empty_photo, 6)
                }
            }

            2 -> {
                vh.imageViewDelete.visibility = View.VISIBLE

                val w = getW(context) / 3
                vh.imageView.layoutParams = FrameLayout.LayoutParams(w, w)
                val picCode = fileInfo.code

                when (picCode) {
                    1 -> {
                        ImageUtil.getInstance().loadRoundCircleImage(context, vh.imageView, fileInfo.picUrl, 0, 6)
                    }
                    2 -> {
                        ImageUtil.getInstance().loadVideoScreenshot(context, fileInfo.videoUrl, vh.imageView, 0, null)
                    }

                    3 -> {
                        vh.imageView.scaleType = ImageView.ScaleType.CENTER
                        vh.imageView.adjustViewBounds = false
                        vh.imageView.setImageBitmap(
                            BitmapFactory.decodeResource(
                                context.resources,
                                R.drawable.play_voice
                            )
                        )
                    }

                    4 -> {
                        /// 音频配图片

                        vh.imageView.scaleType = ImageView.ScaleType.CENTER
                        vh.imageView.adjustViewBounds = false
                        vh.imageView.setImageBitmap(
                            BitmapFactory.decodeResource(
                                context.resources,
                                R.drawable.play_voice
                            )
                        )

                        Log.i("result", "图片地址------" + fileInfo.picUrl)

                        ImageUtil.getInstance().loadByDrawable(context, fileInfo.picUrl, vh.imageView)

                    }
                }

            }

            3 -> {
                vh.imageViewDelete.visibility = View.GONE
                ImageUtil.getInstance().loadCircleImage(context, vh.imageView, fileInfo.text, 0)
            }

            4 -> {
                val w = (getW(context) - context.resources.getDimension(R.dimen.dp_20).toInt()) / 3
                vh.imageView.layoutParams = FrameLayout.LayoutParams(w, w)
                vh.imageViewDelete.visibility = View.GONE
                ImageUtil.getInstance().loadRoundCircleImage(context, vh.imageView, fileInfo.text, 0, 4)
            }
        }

        vh.imageViewDelete.setOnClickListener {
            val ppp = vh.adapterPosition
            (context as SendTieActivity).deleteListId(ppp)

        }

    }

    override fun onCreateView(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = inflater.inflate(R.layout.image_view_layout, parent, false)
        return VH(view)

    }

    override fun onUpdateHolder(holder: RecyclerView.ViewHolder, position: Int, payloads: MutableList<Any>) {

    }


    /**
     *  1 为图片  2 为视频 3 为音频  4 为音频配图片
     */
    fun updateInfo(code: Int, id: String, url: String) {

        Log.i("result", "--------$code")

        val fileInfo = FileInfo()
        fileInfo.code = code
        fileInfo.idStr = id

        when (code) {
            1, 4 -> {
                fileInfo.picUrl = url
            }

            2 -> {
                fileInfo.videoUrl = url
            }
            3 -> {
                fileInfo.voiceUrl = url
            }
        }


        if (code == 4) {

            if (list.size != 0) {

                val ccInfo = list[0]

                if (ccInfo != null) {
                    ccInfo.picUrl = url
                    ccInfo.code = 4
                    notifyDataSetChanged()
                }
            }

        } else {
            list.add(0, fileInfo)
            notifyItemInserted(0)
        }

    }

    fun updateInfoNoCode(fileInfo: FileInfo) {
        list.add(0, fileInfo)
        notifyItemInserted(0)
    }

    inner class VH(view: View) : RecyclerView.ViewHolder(view) {
        val imageView = view.imageViewPic
        val imageViewDelete = view.imageViewDelete

    }
}